<?php
/**
 *    地图控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class MapApp extends MallbaseApp
{
	var $_waybill_mod;
	var $_dictionary_mod;
	var $_user_mod;
	var $canedit;
	var $_haul_mod;
	var $_exception_mod;
	var $_exattr_mod;
	function __construct()
	{
		$this->MapApp();
	}

	function MapApp()
	{
		parent::__construct();
		$this->_waybill_mod =& m('waybill');
		$this->_dictionary_mod =& m('dictionary');
		$this->_user_mod =& m('member');
		$this->_haul_mod =& m('haul');
		$this->_exception_mod =& m('exception');
		$this->_exattr_mod =& m('exattr');
		$this->canedit=false;

	}

	function gpsinfo(){
		$db=&db();
		$wm_tid=empty($_REQUEST['id'])?0:intval($_REQUEST['id']);
		if(empty($wm_tid)) {
			return;
		}
			
		$sql="select gps_latitude as lat,gps_longitude as lng,gps_addtime as utime,gps_gpstime as time,gps_speed as speed,gps_direction as direction from zys_gps where gps_wmtid='".$wm_tid."' ";
		$temp=$db->getAll($sql);
		$data=array();
		foreach ($temp as $key => $v)
		{
			$data[]= $temp[$key];
		}

		echo ecm_json_encode($data);
	}

	/**
	 *    地图
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
		$waybill_mod =& m('waybill');

		$waybill_data = $waybill_mod->get(array(
            'fields'=>'wm_code,wm_type,wm_stauts,wm_carrier,wm_vhc_code,wm_driver,wm_driver_tel,wm_startlng,wm_startlat',
            'conditions' =>"wm_code='". $_GET['code']."'",
            'count' => false
		));
		$this->assign('bill', $waybill_data);
		$this->import_resource(array('style' => 'weui.min.css'));
		$this->assign('carid', $_GET['carid']);
		$this->assign('vhc_code', $waybill_data['wm_vhc_code']);
		$this->assign('id', $waybill_data['wm_tid']);
		$this->assign('code', $_GET['code']);
		$this->assign('gpsurl',CARGPS_URL);
		$this->assign('gpsop',empty($_GET['op'])?'track':'trackall');
		$this->display('map.index.html');
	}

	function mapselect(){
		$lng=empty($_GET['lng'])?"":$_GET['lng'];
		$lat=empty($_GET['lat'])?"":$_GET['lat'];
		$this->assign('ret', $_GET['ret']);
		$this->assign('address', $_GET['address']);
		$this->assign('lng', $_GET['lng']);
		$this->assign('lat', $_GET['lat']);
		$this->display('map.mapselect.html');
	}
	
	//异步数据
	function ajax_haul()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if(empty($id)) {
			return;
		}
		$conditions = "h_wmtid=".$id;
		$data=array();
		$haul_mod =& m('haul');
		$haul_data = $haul_mod->find(array(
            'fields'=>'h_short_name,h_finished,h_order,h_finished_date,h_finished_lng,h_finished_lat,h_mapimage,h_lng,h_lat,h_changecolor',
            'conditions' =>$conditions,
            'count' => false,
            'order' => "h_order"
            ));
            foreach ($haul_data as $key => $haul)
            {
            	$haul_data[$key]['h_finished_date'] = local_date('Y-m-d H:i:s',$haul['h_finished_date']);
            	$data[]= $haul_data[$key];
            }
            echo ecm_json_encode($data);
	}

	//异常展示
	function ajax_exception(){
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if(empty($id)) {
			return;
		}
		$data=array();
		$conditions = "e_w_tid=".$id;
		$exception_mod =& m('exception');
		$exception_data = $exception_mod->find(array(
            'fields'=>'e_staus,e_type,e_date,e_lng,e_lat',
            'conditions' =>$conditions,
            'count' => false,
            'order' => "e_tid"
            ));
            foreach ($exception_data as $key => $exception)
            {
            	$exception_data[$key]['e_date']=local_date('Y-m-d H:i',$exception_data[$key]['e_date']);
            	$data[]= $exception_data[$key];
            }
            echo ecm_json_encode($data);
	}

	function view(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST){
			$waybill=$this->_waybill_mod->get(array(
	        'fields'   => 'this.*,member.user_name,member.real_name',
	        'conditions'  => 'wm_tid='.$id,
			'join'=>'belongs_to_user',
			));
			$db=&db();
			$normal=true;
			if($waybill['wm_type']=='集卡进口'||$waybill['wm_type']=='集卡出口'){
				$normal=false;
				$sql="SELECT * from zys_waybill_sub_trunk w LEFT JOIN zys_goods_trunk_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_trunk t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_companytid']=$v['g_companytid'];
						$sublist[$v['gs_gtid']]['g_ship']=$v['g_ship'];
						$sublist[$v['gs_gtid']]['g_voyage']=$v['g_voyage'];
						$sublist[$v['gs_gtid']]['g_awb_code']=$v['g_awb_code'];
						$sublist[$v['gs_gtid']]['g_packingtime']=$v['g_packingtime'];
						$sublist[$v['gs_gtid']]['g_collectiontime']=$v['g_collectiontime'];
						$sublist[$v['gs_gtid']]['g_container_depot']=$v['g_container_depot'];
						$sublist[$v['gs_gtid']]['g_container_lng']=$v['g_container_lng'];
						$sublist[$v['gs_gtid']]['g_container_lat']=$v['g_container_lat'];
						$sublist[$v['gs_gtid']]['g_container_man']=$v['g_container_man'];
						$sublist[$v['gs_gtid']]['g_container_tel']=$v['g_container_tel'];
						$sublist[$v['gs_gtid']]['g_full_container_depot']=$v['g_full_container_depot'];
						$sublist[$v['gs_gtid']]['g_full_container_lng']=$v['g_full_container_lng'];
						$sublist[$v['gs_gtid']]['g_full_container_lat']=$v['g_full_container_lat'];
						$sublist[$v['gs_gtid']]['g_full_container_man']=$v['g_full_container_man'];
						$sublist[$v['gs_gtid']]['g_full_container_tel']=$v['g_full_container_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_loading_man']=$v['g_loading_man'];
						$sublist[$v['gs_gtid']]['g_loading_tel']=$v['g_loading_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['g_tgdate']=$v['g_tgdate'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}else{
				$sql="SELECT * from zys_waybill_sub_normal w LEFT JOIN zys_goods_normal_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_normal t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_ownertid']=$v['g_ownertid'];
						$sublist[$v['gs_gtid']]['g_owner']=$v['g_owner'];
						$sublist[$v['gs_gtid']]['g_owner_tel']=$v['g_owner_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_unloading_place']=$v['g_unloading_place'];
						$sublist[$v['gs_gtid']]['g_loading_date']=$v['g_loading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_date']=$v['g_unloading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_lng']=$v['g_unloading_lng'];
						$sublist[$v['gs_gtid']]['g_unloading_lat']=$v['g_unloading_lat'];
						$sublist[$v['gs_gtid']]['g_consignee']=$v['g_consignee'];
						$sublist[$v['gs_gtid']]['g_consignee_tel']=$v['g_consignee_tel'];
						$sublist[$v['gs_gtid']]['g_gtype']=$v['g_gtype'];
						$sublist[$v['gs_gtid']]['g_consignor']=$v['g_consignor'];
						$sublist[$v['gs_gtid']]['g_consignor_tel']=$v['g_consignor_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('ctype', $typelist);
			$sql="SELECT * from zys_haul where h_wmtid=".$id." ORDER BY h_order";
			$haul=$db->getAll($sql);

			$haullist=array();
			if($haul){
				foreach ($haul as $v){
					$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
					$collect=$db->getAll($sql);
					if($collect){
						foreach ($collect as $vv){
							$arrs=array();
							if($vv['h_collect_val']){
								$vv['h_collect_val']=trim($vv['h_collect_val'],',');
								$arrs=explode(',', $vv['h_collect_val']);
							}
							$vv['ar']=$arrs;
							$v['item'][]=$vv;
						}
					}

					$haullist[]=$v;
				}
			}
			//异常信息
			$exception=array();
			$sql="SELECT * FROM zys_exception WHERE e_w_tid=$id";
			$exception=$db->getAll($sql);
			if($exception){
				foreach ($exception as $k=>$v){
					$sql="SELECT * FROM zys_exception_attr WHERE ea_type=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_pic']=$db->getAll($sql);

					$sql="SELECT * FROM zys_exception_attr WHERE ea_type!=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_voice']=$db->getAll($sql);
				}
			}
			$this->assign('exception', $exception);
			$this->assign('haullist', $haullist);
			$this->assign('normal', $normal);
			$this->assign('waybill', $waybill);
			$this->assign('sublist', $sublist);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
				'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->display('waybill.view.html');
		}
	}
}
?>
